package com.example.mobile2021606129ly0408;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private CheckBox chkboxMainYinqing;
    private CheckBox chkboxMainBoli;
    private CheckBox chkboxMainFangxiangpan;
    private Button BtnMainBuy;
    private TextView tvMainTip;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //3
        initView();
        chkboxMainYinqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if (isChecked) {
           tvMainResult.setText("选中：" + buttonView.getText().toString());
           //buttonView.getText().toString()用于获取按钮内的文本内容
         } else {
           tvMainResult.setText("取消选中：" + buttonView.getText().toString());
         }
         }
       });
                   chkboxMainBoli.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked) {
                        tvMainResult.setText("选中：" + buttonView.getText().toString());
                    }else {
                        tvMainResult.setText("取消选中：" + buttonView.getText().toString());
                    }
                }
        });
        chkboxMainFangxiangpan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMainResult.setText("选中：" + buttonView.getText().toString());
                } else {
                    tvMainResult.setText("取消选中：" + buttonView.getText().toString());
                }
            }
            });
        BtnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg="选中";
                //依次判断复选框的选中状态
                if(chkboxMainYinqing.isChecked()) {
                    msg = "\n" + chkboxMainYinqing.getText().toString();
                }
                if(chkboxMainBoli.isChecked()) {
                    msg = "\n" + chkboxMainBoli.getText().toString();
                }
                if(chkboxMainFangxiangpan.isChecked()) {
                    msg = "\n" + chkboxMainFangxiangpan.getText().toString();
                }
                tvMainResult.setText(msg);
                }

        });
    }
    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        chkboxMainYinqing = (CheckBox) findViewById(R.id.chkbox_main_yinqing);
        chkboxMainBoli = (CheckBox) findViewById(R.id.chkbox_main_boli);
        chkboxMainFangxiangpan = (CheckBox) findViewById(R.id.chkbox_main_fangxiangpan);
        BtnMainBuy = (Button) findViewById(R.id.Btn_main_buy);
        tvMainTip = (TextView) findViewById(R.id.tv_main_tip);
        tvMainResult = (TextView) findViewById(R.id.tv_main_result);
    }
}